// import Dummy from '@/components/dummy.vue'
import Article from '@/views/article.vue'
import { AlexMsg } from '@2473o/alexui'

const componentMap = {
  Article
}

async function fetchAsyncRoutes(routes, router) {
  const asyncRoutes = [
    {
      parent: 'index',
      path: '/about',
      component: 'Article',
      meta: { title: '关于', params: { id: 'about' } }
    },
    {
      parent: 'index',
      path: '/links',
      component: 'Article',
      meta: { title: '相关链接', params: { id: 'links' } }
    },
    {
      parent: 'index',
      path: '/archives',
      component: 'Article',
      meta: { title: '归档', params: { id: 'archives' } }
    }
  ]

  try {
    asyncRoutes.forEach(async (route) => {
      // 根据路由配置对象找到父路由
      const parentRoute = routes.find((r) => r.name === route.parent)

      if (parentRoute) {
        // 创建子路由配置对象
        const childRoute = {
          path: route.path,
          name: route.path,
          component: componentMap[route.component],
          meta: route.meta,
          params: route.params
        }

        // 添加子路由到父路由的 children 数组中
        if (!parentRoute.children) {
          parentRoute.children = []
        }
        parentRoute.children.push(childRoute)

        // 动态添加子路由
        await router.addRoute(parentRoute, childRoute)
      }
    })
  } catch (error) {
    // console.error('Failed to fetch async routes:', error)
    AlexMsg({
      type: 'error',
      msg: `Failed to fetch async routes ${error}`
    })
  }
}

export default fetchAsyncRoutes
